I am creating a ZIP compressor using the ADM-ZIP library and Multer for uploading the files. The code does work when I hit the download button and I do get the compressed file, but after the server breaks stating "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client"
app.js
const express = require("express");
const multer = require("multer");
const admzip = require("adm-zip");
const fs = require("fs");
const path = require("path");
const app = express();
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(cors());
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "public/uploads");
},
filename: (req, file, cb) => {
cb(
null,
file.filename + "-" + Date.now() + path.extname(file.originalname)
);
},
});
const maxSize = 10 * 1024 * 1024;
const compressFileUpload = multer({
storage,
limits: {
filesize: maxSize,
},
});
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.post("/compress", compressFileUpload.array("file", 100), (req, res) => {
const zip = new admzip();
if (req.files) {
req.files.map(file => {
console.log(file.path);
zip.addLocalFile(file.path);
});
const output = Date.now() + "output.zip";
fs.writeFileSync(output, zip.toBuffer());
res.download(output, (err) => {
res.send("Error in downloading zip file.")
})
}
});
app.listen(process.env.PORT || 5000, () => {
console.log("server running on PORT 5000...");
});